package com.whmskj.xjlhsc.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.collection.CollectionUtil;
import com.whmskj.xjlhsc.controller.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.whmskj.xjlhsc.entity.WbPlan;
import com.whmskj.xjlhsc.service.WbPlanService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 维保计划表
 *
 * @author lk
 * @since 2025-06-10 08:57:31
 */
@RestController
@RequestMapping("/api/wbPlan")
@Api(tags = "维保计划接口", value = "WbPlanController")
@SaIgnore
public class WbPlanController {

    @Autowired
    private WbPlanService wbPlanService;

    /**
     * 分页查询
     *
     * @param params
     * @return
     */
    @GetMapping("/findPage")
    @Operation(description = "维保计划分页查询", method = "GET")
    public Result findPage(WbPlan params) {
        Page<WbPlan> result = wbPlanService.findPage(params);
        return Result.success(result);
    }

    /**
     * 列表查询
     *
     * @param params
     * @return
     */
    @GetMapping("/findList")
    public Result findList(WbPlan params) {
        List<WbPlan> result = wbPlanService.findList(params);
        return Result.success(result);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Long id) {
        WbPlan wbPlan = wbPlanService.findById_c(id);
        return Result.success(wbPlan);
    }

    /**
     * 新增
     *
     * @param wbPlan
     * @return
     */
    @PostMapping
    public Result insert(@RequestBody WbPlan wbPlan) {
        return Result.success(wbPlanService.addWbPlan(wbPlan));
    }

    /**
     * 修改
     *
     * @param wbPlan
     * @return
     */
    @PutMapping
    public Result update(@RequestBody WbPlan wbPlan) {
        return Result.success(wbPlanService.update_c(wbPlan));
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id) {
        wbPlanService.delete_c(id);
        return Result.success();
    }

    @PostMapping("/copy-generator-next")
    @Operation(description = "复制生成下个周期计划")
    @Parameters({
            @Parameter(name = "maintenancePlanIds", description = "维保计划ID列表", required = true)
    })
    public Result copyGeneratorNext(@RequestBody List<Long> maintenancePlanIds) {
        wbPlanService.copyGeneratorNext(maintenancePlanIds);
        return Result.success();
    }

    @GetMapping("/export")
    @Operation(description = "导出维保计划")
    public void export(@RequestBody(required = false) WbPlan params) {
        wbPlanService.export(params, ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder
                .getRequestAttributes())).getResponse());
    }


}